package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    @Autowired
    private MinIoTemplate minIoTemplate;

    //图片上传
    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        //获取上传图片的输出流
        InputStream inputStream = multipartFile.getInputStream();

        // 文件名构成：不重复的文件名 + 文件后缀
        // images/png 分隔 获取后面的内容
        String contentType = multipartFile.getContentType();
        if (StringUtils.isEmpty(contentType)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String[] split = contentType.split("/");
        String suffixName = split[1];

        //图片上传到mino
//        String url = minIoTemplate.uploadFile("", System.currentTimeMillis() + "." + suffixName, inputStream, "image");
        String fileName = System.currentTimeMillis() + "." + suffixName;
        String url = minIoTemplate.uploadFile("", fileName, inputStream, "image");
        //将上传的url 写入到数据库，material这张表
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(url);
        wmMaterial.setUserId(UserThreadLocal.get().getId());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        wmMaterial.setCreatedTime(new Date());
        int insert = wmMaterialMapper.insert(wmMaterial);
        if (insert < 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //将创建好的素材信息，返回给前端

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public PageResponseResult list(WmMaterialDto dto) {
        if (dto == null) {
            return new PageResponseResult(0, 0, 0);
        }
        //查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getIsCollection() == 1, WmMaterial::getIsCollection, dto.getIsCollection());

        //分页条件
        Integer dtoPage = dto.getPage();
        Integer size = dto.getSize();
        Page<WmMaterial> page = new Page(dtoPage, size);
        //执行分页查询
        Page pageResult = wmMaterialMapper.selectPage(page, wrapper);

        //构建返回值结构
        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(dtoPage, size, total.intValue());

        pageResponseResult.setData(pageResult.getRecords());

        return pageResponseResult;
    }
}
